package com.gnerv.rabbitmq.agent.handler.v_3_7;

import com.gnerv.rabbitmq.agent.handler.AbstractClusterStatusExecResultHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @author Gnerv LiGen
 */
@Slf4j
public class ClusterStatusExecResultHandler extends AbstractClusterStatusExecResultHandler {

    private static final Pattern NAME = Pattern.compile("\\{cluster_name\\,\\<\\<\\\"(\\S*)\\\"\\>\\>\\}");
    private static final Pattern DISC = Pattern.compile("\\{disc\\,\\[(\\S*?)\\]\\}");
    private static final Pattern RAM = Pattern.compile("\\{ram\\,\\[(\\S*?)\\]\\}");
    private static final Pattern RUNNING_NODES = Pattern.compile("\\{running_nodes\\,\\[(\\S*?)\\]\\}");
    private static final Pattern VERSION = Pattern.compile("");

    @Override
    protected void name() {
        Map<Integer, String> matcher = matcher(NAME, 1);
        String name = matcher.get(1);
        clusterStatus.setName(name);
    }

    @Override
    protected void disc() {
        Map<Integer, String> matcher = matcher(DISC, 1);
        String disc = matcher.get(1);
        if (StringUtils.hasText(disc)) {
            List<String> list = Arrays.stream(disc.split(",")).toList();
            clusterStatus.setDisc(list);
        }
    }

    @Override
    protected void ram() {
        Map<Integer, String> matcher = matcher(RAM, 1);
        String ram = matcher.get(1);
        if (StringUtils.hasText(ram)) {
            List<String> list = Arrays.stream(ram.split(",")).toList();
            clusterStatus.setRam(list);
        }
    }

    @Override
    protected void running() {
        Map<Integer, String> matcher = matcher(RUNNING_NODES, 1);
        String running = matcher.get(1);
        if (StringUtils.hasText(running)) {
            List<String> list = Arrays.stream(running.split(",")).toList();
            clusterStatus.setRunning(list);
        }
    }
}
